﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using JetBrains.Annotations;

namespace WikiDoc.Lines
{
    /// <summary>
    /// 
    /// </summary>
    public class TableLine : WikiLine
    {
        public TableLine(string line, string content) : base(line, LineTypes.Table)
        {
            Content = content;
        }

        /// <summary>
        /// 去掉头尾空白后的内容
        /// </summary>
        public string Content { get; private set; }

        /// <summary>
        /// 表格行定义 连续两个竖线开头并结尾
        /// </summary>
        public static readonly Regex Pattern = new Regex(
            @"\A (\s*)  (\|\|)  (.*) \|\| \s* \z",
            RegexOptions.Compiled |
            RegexOptions.CultureInvariant |
            RegexOptions.Singleline |
            RegexOptions.IgnorePatternWhitespace);

        [CanBeNull]
        public static WikiLine TryParse(string line)
        {
            if (line == null) return null;

            var match = Pattern.Match(line);

            return match.Success
                       ? new TableLine(line, match.Groups[3].Value)
                       : null;
        }
    }
}
